<!-- 横向布局 -->
<template>
  <el-container class="layout">
    <el-header>
      <div class="logo flx-center">
        <img class="logo-img" src="@/assets/images/logo.png" alt="logo" />
        <span class="logo-text">{{ title }}</span>
      </div>
      <!-- <ToolBarLeft /> -->
      <el-menu mode="horizontal" :router="false" :default-active="activeMenu">
        <!-- 不能直接使用 SubMenu 组件，无法触发 el-menu 隐藏省略功能 -->
        <template v-for="subItem in menuList" :key="subItem.path">
          <!-- <el-sub-menu v-if="subItem.children?.length" :key="subItem.path" :index="subItem.path + 'el-sub-menu'">
            <template #title>
              <el-icon>
                <component :is="subItem.meta.icon"></component>
              </el-icon>
              <span>{{ subItem.meta.title }}</span>
            </template>
            <SubMenu :menu-list="subItem.children" />
          </el-sub-menu> -->
          <el-menu-item :index="subItem.path" @click="handleClickMenu(subItem)">
            <el-icon>
              <component :is="subItem.meta.icon"></component>
            </el-icon>
            <template #title>
              <span>{{ subItem.meta.title }}</span>
            </template>
          </el-menu-item>
        </template>
      </el-menu>
      <ToolBarRight />
    </el-header>
    <el-container class="classic-content">
      <el-aside>
        <div class="aside-box" :style="{ width: isCollapse ? '0px' : '230px' }">
          <el-scrollbar>
            <el-menu
              :router="false"
              :default-active="activeMenu"
              :collapse="isCollapse"
              :unique-opened="accordion"
              :collapse-transition="false"
            >
              <SubMenu :menu-list="childrenMenuList" />
            </el-menu>
          </el-scrollbar>
        </div>
      </el-aside>
      <el-container class="classic-main">
        <Main />
      </el-container>
    </el-container>
  </el-container>
</template>

<script setup lang="ts" name="layoutTransverse">
import { computed } from "vue";
import { useAuthStore } from "@/stores/modules/auth";
import { useRoute, useRouter } from "vue-router";
import Main from "@/layouts/components/Main/index.vue";
import ToolBarRight from "@/layouts/components/Header/ToolBarRight.vue";
import SubMenu from "@/layouts/components/Menu/SubMenu.vue";
import { useGlobalStore } from "@/stores/modules/global";
import { useMenusStore } from "@/stores/modules/menu";
// import { list } from "@/utils/list";
// import ToolBarLeft from "@/layouts/components/Header/ToolBarLeft.vue";
import { ref } from "vue";

const title = import.meta.env.VITE_GLOB_APP_TITLE;
const menusStore = useMenusStore();
const route = useRoute();
const router = useRouter();
const authStore = useAuthStore();
const menuList = computed(() => authStore.showMenuListGet);
// const menuList = ref([])
// menuList.value = list
function removeChildren(arr) {
  arr.forEach(obj => {
    // 删除当前对象中的children属性
    delete obj.children;

    // 如果当前对象还有子对象（children），递归调用
    if (obj.children && Array.isArray(obj.children)) {
      removeChildren(obj.children);
    }
  });
}
// removeChildren(menuList.value)
console.log('menuList',menuList.value);

const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.path) as string);
const globalStore = useGlobalStore();
const isCollapse = computed(() => globalStore.isCollapse);
const accordion = computed(() => globalStore.accordion);

let menuStatus = ref(false)
let childrenList = ref([])
const childrenMenuList = computed(() => menusStore.childrenMenus);
const handleClickMenu = (subItem: Menu.MenuOptions) => {
  if (subItem.meta.isLink) return window.open(subItem.meta.isLink, "_blank");
  console.log('subItem',subItem.children);
  if(subItem.children) {
    childrenList.value = findChildrenByName(menuList.value, subItem.name)
    // menuStatus.value = true
    globalStore.setGlobalState("isCollapse", false);
    menusStore.saveChildrenMenus(childrenList.value);
  } else {
    router.push(subItem.path);
    globalStore.setGlobalState("isCollapse", true);
    // menuStatus.value = false
  }
  // if (subItem.meta.isLink) return window.open(subItem.meta.isLink, "_blank");
  // router.push(subItem.path);
};
function findChildrenByName(arr, targetName) {
  for (let obj of arr) {
    // 如果当前对象的 name 匹配
    if (obj.name === targetName) {
      return obj.children || [];  // 返回 children 属性，如果没有则返回空数组
    }
    // 如果当前对象有 children，递归查找
    if (obj.children && Array.isArray(obj.children)) {
      let result = findChildrenByName(obj.children, targetName);
      if (result) {
        return result;
      }
    }
  }

  // 如果没有找到，返回 undefined
  return undefined;
}
</script>

<style scoped lang="scss">
@import "./index.scss";
</style>
